/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpotifyNews.java
 *
 * Created on 1-nov-2010, 22:30:33
 */

package spotifynews;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;

/**
 *
 * @author Roan
 */
public class SpotifyNews extends javax.swing.JFrame {

    ProgramLoopThread programLoop;

    /** Creates new form SpotifyNews */
    public SpotifyNews() {
        initComponents();
        programLoop = new ProgramLoopThread(this);
        programLoop.startLoop();
        programLoop.setPlayNewsEveryHour(true);
        addToTray();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblStatus = new javax.swing.JLabel();
        chkAutoEveryHour = new javax.swing.JCheckBox();
        btnGoPlayNews = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuBestand = new javax.swing.JMenu();
        menuExit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("SpotifyNews");
        setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/nos.png")));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowIconified(java.awt.event.WindowEvent evt) {
                formWindowIconified(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Status"));

        lblStatus.setText("Spotify is gevonden.");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStatus)
                .addContainerGap(312, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        chkAutoEveryHour.setSelected(true);
        chkAutoEveryHour.setText("Pauzeer Spotify elk uur en speel het laatste nieuws af");
        chkAutoEveryHour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkAutoEveryHourActionPerformed(evt);
            }
        });

        btnGoPlayNews.setText("Beluister na dit nummer het laatste nieuws");
        btnGoPlayNews.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGoPlayNewsActionPerformed(evt);
            }
        });

        btnStop.setText("Stop het nieuws");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        menuBestand.setText("Bestand");

        menuExit.setText("Sluiten");
        menuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuExitActionPerformed(evt);
            }
        });
        menuBestand.add(menuExit);

        jMenuBar1.add(menuBestand);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkAutoEveryHour)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGoPlayNews)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStop)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(chkAutoEveryHour)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGoPlayNews)
                    .addComponent(btnStop))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chkAutoEveryHourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAutoEveryHourActionPerformed
        programLoop.setPlayNewsEveryHour(chkAutoEveryHour.isSelected());
        if(chkAutoEveryHour.isSelected()){
            setStatus("NOS headlines zal vanaf nu elk uur worden afgespeeld");
        } else {
            setStatus("NOS headlines zal vanaf nu niet meer automatisch worden afgespeeld");
        }
    }//GEN-LAST:event_chkAutoEveryHourActionPerformed

    private void btnGoPlayNewsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGoPlayNewsActionPerformed
        programLoop.goPlayNews();
    }//GEN-LAST:event_btnGoPlayNewsActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        programLoop.continuePlaying();
    }//GEN-LAST:event_btnStopActionPerformed

    private void menuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExitActionPerformed
        programLoop.stopLoop();
        
        System.exit(0);
    }//GEN-LAST:event_menuExitActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        programLoop.stopLoop();

        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    private void formWindowIconified(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowIconified
        this.setVisible(false);
    }//GEN-LAST:event_formWindowIconified


    public void setStatus(String message){
        lblStatus.setText(message);
    }


    private void addToTray() {
        final TrayIcon trayIcon;

        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/nos.png"));

            

            ActionListener trayListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(e.getActionCommand().equals("Exit")){
                        programLoop.stopLoop();
                        System.exit(0);
                    } else if(e.getActionCommand().equals("Nieuws afspelen na dit nummer")){

                        programLoop.goPlayNews();
                    } else if(e.getActionCommand().equals("Nu de laatste NOS headlines afspelen")){
                        programLoop.playNews();
                    } else if(e.getActionCommand().equals("Afspelen van het huidige nieuws stoppen")){
                        programLoop.stopNews();
                    }
                }
            };

            PopupMenu popup = new PopupMenu();
            MenuItem exitItem = new MenuItem("Exit");
            exitItem.addActionListener(trayListener);
            popup.add(exitItem);
            MenuItem playNewsItem = new MenuItem("Nieuws afspelen na dit nummer");
            playNewsItem.addActionListener(trayListener);
            popup.add(playNewsItem);
            MenuItem playNewsNowItem = new MenuItem("Nu de laatste NOS headlines afspelen");
            playNewsNowItem.addActionListener(trayListener);
            popup.add(playNewsNowItem);
            MenuItem stopNewsNowItem = new MenuItem("Afspelen van het huidige nieuws stoppen");
            stopNewsNowItem.addActionListener(trayListener);
            popup.add(stopNewsNowItem);


            trayIcon = new TrayIcon(image, "SpotifyNews", popup);

            MouseListener mouseListener = new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    
                }

                public void mouseEntered(MouseEvent e) {
                    
                }

                public void mouseExited(MouseEvent e) {
                }

                public void mousePressed(MouseEvent e) {
                }

                public void mouseReleased(MouseEvent e) {
                }
            };
            
            ActionListener actionListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if(isVisible()){
                        setExtendedState(ICONIFIED);
                        setVisible(false);
                    } else {
                        setVisible(true);
                        setExtendedState(NORMAL);
                    }
                }
            };

            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("TrayIcon could not be added.");
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            System.err.println("Windows Look-and-feel cannot be loaded, continue using default Look-and-feel");
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SpotifyNews().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGoPlayNews;
    private javax.swing.JButton btnStop;
    private javax.swing.JCheckBox chkAutoEveryHour;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JMenu menuBestand;
    private javax.swing.JMenuItem menuExit;
    // End of variables declaration//GEN-END:variables

}
